package de.bht.mi.kriz.multitouch.components;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Paint.Style;

public class ContainerElement extends MultiTouchViewElement {

	public final static int SCALE_HANDLE_LEFT = 200;
	public final static int SCALE_HANDLE_RIGHT = 300;
	public final static int CONTENT = 400;
	
	public ContainerElement(Context context) {
		super(context);
		initializeElement(context);
	}
	
	public ContainerElement(Context context, Point p) {
		super(context, p);
		localPosition.y += TITLE_SPACE;
		initializeElement(context);
	}
	
	public ContainerElement(Context context, int x, int y) {
		super(context, x, y);
		initializeElement(context);
	}
	
	@Override
	protected void initializeElement(Context context) {
		TITLE_SPACE = 15;
		MIN_SIZE = 60;
		super.initializeElement(context);
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		canvas.translate(globalPosition.x, globalPosition.y);
		
		float strokeWidth = 0;
		
		Paint pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
		pathPaint.setStyle(Style.STROKE);
		pathPaint.setStrokeWidth(strokeWidth);
		pathPaint.setColor(this.drawColor);
		pathPaint.setAlpha(getAlpha());
		
		Path pth = new Path();
		pth.moveTo(0, TITLE_SPACE);
		pth.lineTo(0, TITLE_SPACE+height);
		pth.lineTo(width, TITLE_SPACE+height);
		pth.lineTo(width, TITLE_SPACE);
		pth.close();
		canvas.drawPath(pth, pathPaint);
		
		pathPaint.setStyle(Style.FILL_AND_STROKE);
		pth.reset();
		pth.moveTo(0,0);
		pth.lineTo(0, TITLE_SPACE);
		pth.lineTo(width, TITLE_SPACE);
		pth.lineTo(width, 0);
		pth.close();
		canvas.drawPath(pth, pathPaint);
		
		Paint textPaint = new Paint();
		textPaint.setColor(0xff000000);
		textPaint.setAlpha(getAlpha());
		
		Rect bounds = new Rect();
		int msgLen = this.title.length();
		textPaint.getTextBounds(this.title, 0, msgLen, bounds);
		
		canvas.drawText(this.title, strokeWidth, strokeWidth+bounds.height(), textPaint);
		canvas.translate(-globalPosition.x, -globalPosition.y);
	}

	
	//checks which view-component-part is at the world coordinate located point
	public int check(float x, float y) 
	{
		if(x >= globalPosition.x-GRAB_TOLLERANCE*3 && x <= globalPosition.x+GRAB_TOLLERANCE*3 && y >= globalPosition.y+TITLE_SPACE+height-GRAB_TOLLERANCE*3 && y <= globalPosition.y+TITLE_SPACE+height+GRAB_TOLLERANCE*3)
		{
			return SCALE_HANDLE_LEFT;
		} 
		else if(x >= globalPosition.x+width-GRAB_TOLLERANCE*3 && x <= globalPosition.x+width+GRAB_TOLLERANCE*3 && y >= globalPosition.y+TITLE_SPACE+height-GRAB_TOLLERANCE*3 && y <= globalPosition.y+TITLE_SPACE+height+GRAB_TOLLERANCE*3)
		{
			return SCALE_HANDLE_RIGHT;
		}
		else if(x >= globalPosition.x-GRAB_TOLLERANCE && x <= globalPosition.x+width+GRAB_TOLLERANCE && y >= globalPosition.y-GRAB_TOLLERANCE && y <= globalPosition.y+TITLE_SPACE+GRAB_TOLLERANCE)
		{
			return MOVE_HANDLE;
		}
		else if(x >= globalPosition.x && x <= globalPosition.x+width && y >= globalPosition.y && y <= globalPosition.y+height)
		{
			return CONTENT;
		}
		
		return super.check(x, y);
	}
}